﻿using System.Collections.Generic;
using System.Web;
using FrienDev.Data;

namespace FrienDev.BusinessRules
{
    public class OnlineRules
    {
        public static OnlineRules Instance = new OnlineRules();

        private FrienDevDataContext m_DataContext = FrienDevDataContext.Instance;

        /// <summary>
        /// 在线用户的UserId集合，使用效率最高的HashSet<>
        /// </summary>
        private HashSet<string> m_OnlineUsers = new HashSet<string>();

        public HashSet<string> OnlineUsers
        {
            get
            {
                return m_OnlineUsers;
            }
        }

        private OnlineRules()
        {
        }

        /// <summary>
        /// 用户登录
        /// </summary>
        /// <param name="userId"></param>
        public void UserLogin(string userId)
        {
            HttpContext.Current.Session[ApplicationSettings.SESSIONUSERIDKEY] = userId;
            m_OnlineUsers.Add(userId);
            UserRules.Instance.UpdateUserLoginInfomation();
        }

        /// <summary>
        /// 用户退出
        /// </summary>
        /// <param name="userId"></param>
        public void UserLogout(string userId)
        {
            HttpContext.Current.Session[ApplicationSettings.SESSIONUSERIDKEY] = null;
            m_OnlineUsers.Remove(userId);
        }

        /// <summary>
        /// 使用是否在线
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>
        public bool IsUserOnline(string userId)
        {
            return m_OnlineUsers.Contains(userId);
        }

        /// <summary>
        /// 直接成为好友
        /// </summary>
        public void BecomeFriend()
        {
            if (HttpContext.Current.Session["InviteUserID"] != null && HttpContext.Current.Session["IsInviteeRegisted"] != null)
            {
                User user = UserRules.Instance.IsUserRegisted();
                if (user != null)
                {
                    FriendRules.Instance.AcceptFriend(HttpContext.Current.Session["InviteUserID"].ToString(), UserRules.Instance.GetCurrentUserId());
                    HttpContext.Current.Session.Remove("InviteUserID");
                    HttpContext.Current.Session.Remove("IsInviteeRegisted");
                }
            }
        }

        /// <summary>
        /// 查看活动详情
        /// </summary>
        public void ViewActivity()
        {
            if (HttpContext.Current.Session["ActivityID"] != null && HttpContext.Current.Session["IsInviteeRegisted"] != null)
            {
                User user = UserRules.Instance.IsUserRegisted();
                if (user != null)
                { 
                    HttpResponse res = HttpContext.Current.Response;
                    int activityId=-1;
                    if (int.TryParse(HttpContext.Current.Session["ActivityID"].ToString(), out activityId))
                    {
                        HttpContext.Current.Session.Remove("ActivityID");
                        HttpContext.Current.Session.Remove("IsInviteeRegisted");
                        res.Redirect("~/Activity/detail.aspx?activityid=" + activityId);
                    }
                    res.End();
                }
            }
        }
    }
}
